/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.crud.conclusion;

import java.awt.Frame;
import javax.swing.JDialog;
import systole.domain.report.template.Conclusion;
import systole.exceptions.ExceptionDAO;
import systole.utils.ImageUtils;
import systole.view.crud.controller.ControllerEntityEdition;
import systole.view.messages.EntityDescriptions;

/**
 *
 * @author jmj
 */
public class ControllerConclusionEdition extends ControllerEntityEdition {

    /**
     * 
     */
    public ControllerConclusionEdition() {
        super();
        this.entityName = EntityDescriptions.CONCLUSION;
        this.newDescription = "Nueva";
    }

    @Override
    protected void loadEntityOnForm() {
        Conclusion conclusionToLoad = (Conclusion) this.curretnEntity;
        JDialogConclusion formToLoad = (JDialogConclusion) this.editForm;

        formToLoad.getjEdtName().setText(conclusionToLoad.getName());
        formToLoad.getjEdtConclusion().setText(conclusionToLoad.getText());
        formToLoad.getjChkFemale().setSelected(conclusionToLoad.isFemale());
        formToLoad.getjChkMale().setSelected(conclusionToLoad.isMale());
    }

    @Override
    protected void loadEntityFromForm() {
        Conclusion conclusionToLoad = (Conclusion) this.curretnEntity;
        JDialogConclusion formToLoad = (JDialogConclusion) this.editForm;

        conclusionToLoad.setName(formToLoad.getjEdtName().getText());
        conclusionToLoad.setText(formToLoad.getjEdtConclusion().getText());
        conclusionToLoad.setFemale(formToLoad.getjChkFemale().isSelected());
        conclusionToLoad.setMale(formToLoad.getjChkMale().isSelected());
    }

    @Override
    protected String valid() {
        JDialogConclusion formToLoad = (JDialogConclusion) this.editForm;
        Conclusion conclusion = (Conclusion) this.curretnEntity;
        if ((formToLoad.getjEdtName().getText() == null) || (formToLoad.getjEdtName().getText().isEmpty())) {
            return "Debe ingresar el nombre de la conclusión";
        }

        try {
            if (this.facadeDB.getConclusionBroker().existConclusion(formToLoad.getjEdtName().getText(), conclusion.getId())) {
                return "Ya existe una conclusión con el nombre ingresado";
            }
        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            return ex.getMessage();
        }

        if ((formToLoad.getjEdtConclusion().getText() == null) || (formToLoad.getjEdtConclusion().getText().isEmpty())) {
            return "Debe ingresar la conclusión";
        }

        return null;
    }

    @Override
    protected void save() throws ExceptionDAO {
            if (this.isEditing()) {
                this.facadeDB.getConclusionBroker().update((Conclusion) this.curretnEntity);
            } else {
                this.facadeDB.getConclusionBroker().insert((Conclusion) this.curretnEntity);
            }
    }

    @Override
    protected void loadIconOnForm() {
        this.editForm.setIconImage(ImageUtils.buildImage("resources/icons/conclusion/conclusion16.png"));
    }

    @Override
    protected Object createEntity() {
        return new Conclusion();
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogConclusion(parent, this);
    }
}
